package com.muy.redis.view.component;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManagerListener;

/**
 * @Author jiyanghuang
 * @Date 2023/9/15 20:10
 */
public class MrRedisComponent implements ContentManagerListener {

    private static final Logger LOGGER = Logger.getInstance(MrRedisComponent.class);

    private Project project;

    /**
     * 当前窗口
     */
    private ToolWindow toolWindow;


    protected MrRedisComponent(Project project) {
        this.project = project;
    }

    public static MrRedisComponent getInstance(Project project) {
        return project.getService(MrRedisComponent.class);
    }

    /**
     * @param toolWindow
     * @return
     */
    public MrRedisTab initComponent(ToolWindow toolWindow) {
        toolWindow.getContentManager().addContentManagerListener(this);

        Content content;
        ContentFactory contentFactory = ApplicationManager.getApplication().getService(ContentFactory.class);
        MrRedisTab mrRedisTab = new MrRedisTab(project);
        content = contentFactory.createContent(mrRedisTab, "", false);
        toolWindow.getContentManager().addContent(content);

        toolWindow.activate(new Thread(), true);
        return mrRedisTab;
    }
}
